import prompt from '@system.prompt';
import router from '@system.router';
import data_storage from '@ohos.data.storage';


const PATH = '/data/data/com.pvj.addresslistdemo/main_list'
const TAG = '[index_page]';
const key = 'key_list';
let store;


export default {
    data: {
        LETTERLIST: ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
        list_data: [],
        def_data: [{
                       item_icon: "/common/images/avatar01.png",
                       item_name: '张三',
                       item_phone: '18888888888',
                       item_section: 'Z',
                   },
                   {
                       item_icon: "/common/images/avatar14.png",
                       item_name: '李四',
                       item_phone: '1999999999',
                       item_section: 'L',
                   }, {
                       item_icon: "/common/images/avatar14.png",
                       item_name: '李大爷',
                       item_phone: '1999999999',
                       item_section: 'L',
                   },
                   {
                       item_icon: "/common/images/avatar34.png",
                       item_name: '王五',
                       item_phone: '17777777777',
                       item_section: 'W',
                   }
        ],
        title: "",
        icon_search: "/common/images/search.svg",
        javaInterface: {}
    },
    onInit() {
        console.log(TAG + " ;onInit())");
        this.title = this.$t('strings.world');
        this.def_data.forEach(element => {
            this.list_data.push(element);
        });

        console.log(TAG + " ;getStorage() PATH:" + PATH);
        data_storage.getStorage(PATH, function (err, data) {
            store = data;
            console.log(TAG + " ;getStorage() err:" + err);
            console.log(TAG + " ;getStorage() store:" + store);
        });


    },
    onShow() {
        console.log(TAG + " ;onShow()");

        // onInit生命周期中Java接口对象还未创建完成，请勿在onInit中调用。
        this.javaInterface = createLocalParticleAbility('com.pvj.addresslistdemo.MyLocalParticleAbility');

    },

    onReady() {
        console.log(TAG + " ;onReady())");
    },
    onItemClick(item) {
        console.log(TAG + " ;onItemClick:" + item.item_name);
        prompt.showToast({
            message: "value: " + item.item_name,
            duration: 3000,
        });
        this.navigateRouter(item);
    },
    onItemLongPress(item) {
        console.log(TAG + " ;onItemLongPress:" + item.item_name);
        let THIS = this;
        //点击删除时弹出对话框
        prompt.showDialog({
            title: "操作提示",
            message: "添加" + item.item_name + "为紧急联系人吗?",
            buttons: [{
                          "text": "确定",
                          "color": ""
                      },
                      {
                          "text": "取消",
                          "color": ""
                      }],
            success: function (data) {
                if (data.index == 0) {
//                    prompt.showToast({
//                        message: "确定",
//                        duration: 3000
//                    })

                    THIS.addContactPersonList(item);

                } else {
//                    prompt.showToast({
//                        message: "取消",
//                        duration: 3000
//                    })
                  //  THIS.getContactPersonList();


                }
            }
        });
    },
    navigateRouter(item) {
        console.log(TAG + " ;navigateRouter:" + item.item_name);
        prompt.showToast({
            message: "value: " + item.item_name,
            duration: 3000,
        });

        //鸿蒙带参数跳转页面
        router.push({
            uri: `pages/addressinfo/addressinfo`,
            params: {
                addressItem: item
            }
        });
    },
    onAddButton() {
        var random = randomNum(0, this.LETTERLIST.length - 1);
        var charL = this.LETTERLIST[random];

        let name = getRandomName(2);
        console.log(TAG + " ;onAddButton random:" + name);
        var one = {
            item_icon: "/common/images/avatar14.png",
            item_name: charL + name,
            item_phone: '1999999999',
            item_section: charL
        }

        this.list_data.push(
            one
        )

        this.$element("address_list");

        console.log(TAG + " ;onAddButton:" + this.list_data.length);
    },
    onDeleteButton() {
        if (this.list_data.length == 0) {
            return;
        }
        var random = randomNum(0, this.list_data.length - 1);

        console.log(TAG + " ;onDeleteButton random:" + random);
        this.list_data.splice(random, 1);
    },
    onContactButton() {
        //鸿蒙带参数跳转页面
        router.push({
            uri: `pages/contact/contact`,
            params: {}
        });
    }
     ,
    async addContactPersonList(item) {
        let content =  await this.getContactPersonList();
        console.info(TAG + "addContactPersonList content: " + content);

        let list = []
        if(content != ""){
            list = JSON.parse(content);
        }
        list.push(item);
        let temp = JSON.stringify(list);
        console.info(TAG + "addContactPersonList temp: " + temp);

        this.javaInterface.addContactPersonList(temp).then();
        return true
        // store.putSync(key, temp);
    },
    async getContactPersonList() {

        let ret = await this.javaInterface.getContactPersonList()
        console.info(TAG + "getContactPersonList ret:" + ret);

        return ret

        //    let ret = store.getSync(key, "");
        //    console.info(TAG + "getContactPersonList ret: " + ret);
        //    let array = []
        //    if (ret == "") {
        //        console.info(TAG + "getContactPersonList ret == ");
        //        return array
        //    }
        //
        //    let data = JSON.parse(ret);
        //    return data;

    }


}

//生成 [n,m] 的随机整数。
var randomNum = function (minNum, maxNum) {
    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
}

// 获取指定范围内的随机数
function randomAccess(min, max) {
    return Math.floor(Math.random() * (min - max) + max)
}

// 解码
function decodeUnicode(str) {
    //Unicode显示方式是\u4e00
    str = "\\u" + str
    str = str.replace(/\\/g, "%");
    //转换中文
    str = unescape(str);
    //将其他受影响的转换回原来
    str = str.replace(/%/g, "\\");
    return str;
}

/*
*@param Number NameLength 要获取的名字长度
*/
function getRandomName(NameLength) {
    let name = ""
    for (let i = 0;i < NameLength; i++) {
        let unicodeNum = ""
        unicodeNum = randomAccess(0x4e00, 0x9fa5).toString(16)
        name += decodeUnicode(unicodeNum)
    }
    return name
}

//function addContactPersonList(item) {
//    let list = getContactPersonList();
//    let list_temp = JSON.stringify(list);
//    console.info(TAG + "addContactPersonList list_temp: " + list_temp);
//    list.push(item);
//    let temp = JSON.stringify(list);
//    console.info(TAG + "addContactPersonList temp: " + temp);
//    store.putSync(key, temp);
//}




